#include <string.h>
#include <iostream>
#include "chatmessagetask.h"
#include "../thirdparty/rapidjson/document.h"
#include "../app/mainapp.h"

namespace QinChat{
    namespace Chat{
        CChatMessageTask::CChatMessageTask(std::shared_ptr<Net::CSession> pSeesion, char *message, int len):Core::CTask(), m_pSeesion(pSeesion){
            memcpy(m_message, message, len );
        }

        CChatMessageTask::~CChatMessageTask(){

        }

        void CChatMessageTask::Exect(){
            // {"from":"test12345","to":"test123455","type":"login","content":{"user":"test12345", "password":"testaasdf"}}
            rapidjson::Document docment;
            if (docment.Parse(m_message).HasParseError()){
                return ;
            }

            if(!docment.HasMember("type")){
                return;
            }

            std::string strType = docment["type"].GetString();
            std::cout<<"message: " << strType << std::endl;
            
            CChatMessageHandle* pChatMessageHandel = App::CMainApp::GetInstace().GetChatMessageHandleRegist().GetMessageHandel(strType);
            if (nullptr == pChatMessageHandel){
                std::cout<<"messageHandel is null: " << strType << std::endl;
                return;
            }

            pChatMessageHandel->HandelMessage(m_pSeesion, docment);
        }
    }
}